package com.blindBox.front.handler.verify.registerVerifyImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blindBox.front.commonResult.MsgBackToFront;
import com.blindBox.front.dao_pojo.User;
import com.blindBox.front.handler.verify.Verify;
import com.blindBox.front.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

//检验注册时用户名是否重复
//是否是同一个ip重复进行注册
@Component("userRegisterByAccountVerify")
public class UserRegisterByAccountVerify implements Verify<MsgBackToFront> {
    @Autowired
    UserService userService;
    //检验qq邮箱格式
    private final String qq_format="[1-9]([0-9]{4,10})@qq.com";
    @Override
    public MsgBackToFront verify(Object obj)
    {
        User user = (User) obj;
        //检验输入的邮箱格式是否正确
        if(!user.getMail().matches(qq_format))
        {
            return new MsgBackToFront(null,MsgBackToFront.MAIL_FORMAT_ERROR);
        }
        if(userService.getOne(new QueryWrapper<User>().eq("mail", user.getMail()))!=null)
        {
            //邮箱重复注册
            return new MsgBackToFront(null,MsgBackToFront.MAIL_REPEAT);
        }
        //查找用户名是否已经存在
        //重复:返回true
        //不重复:返回false
       if(userService.getOne(new QueryWrapper<User>().eq("uname", user.getUname()))!=null)
           return new MsgBackToFront(null,MsgBackToFront.REGISTER_REPEAT);
        //没有问题
        return new MsgBackToFront(null,MsgBackToFront.HANDLE_SUCCESS);
    }
}
